﻿<?php 
$date = date("Y-m-d");
$date = strtotime(date("Y-m-d", strtotime($date)) . "-12 months");
$m_start = date("Y-m-d",$date);
$m_end=date("Y-m-d");
?>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js"></script>
<link rel="stylesheet" type="text/css" href="<?php echo base_url('css/jsDatePick_ltr.min.css') ?>" />
<script type="text/javascript" src="<?php echo base_url('js/jsDatePick.min.1.3.js') ?>"></script>

<link rel="stylesheet" type="text/css" href="<?php echo base_url('css/jquery.multiselect.css') ?>" />
<link rel="stylesheet" type="text/css" href="<?php echo base_url('css/jquery.multiselect.filter.css') ?>" />
<link rel="stylesheet" type="text/css" href="<?php echo base_url('assets/style.css') ?>" />
<link rel="stylesheet" type="text/css" href="<?php echo base_url('assets/prettify.css') ?>" />
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/ui-lightness/jquery-ui.css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js"></script>
<script type="text/javascript" src="<?php echo base_url('css/src/jquery.multiselect.js') ?>"></script>
<script type="text/javascript" src="<?php echo base_url('css/src/jquery.multiselect.filter.js') ?>"></script>
<script type="text/javascript" src="<?php echo base_url('assets/prettify.js') ?>"></script>

<script type="text/javascript" src="<?php echo base_url('js/jquery.price_format.1.7.js') ?>"></script>
<script type="text/javascript" src="<?php echo base_url('js/jquery.price_format.1.7.min.js') ?>"></script>

<script type="text/javascript"	src="<?php echo base_url('admin/ckeditor_351/ckeditor.js') ?>"></script>
<script type="text/javascript"	src="<?php echo base_url('admin/ckfinder/ckfinder.js') ?>"></script>


<style>

#cke_dadvantage .cke_contents {
	height: 200px !important;
}
#cke_dcondition .cke_contents {
	height: 200px !important;
}
#cke_dcontent .cke_contents {
	height: 600px !important;
}
</style>

<script type="text/javascript">
window.onload = function(){
	var editor = CKEDITOR.replace("dadvantage",{language:"vi", height:'100%', width:'100%'});
	CKFinder.setupCKEditor(editor, 'admin/ckfinder') ;
	var editor = CKEDITOR.replace("dcondition",{language:"vi", height:'100%', width:'100%'});
	CKFinder.setupCKEditor(editor, 'admin/ckfinder') ;
	var editor = CKEDITOR.replace("dcontent",{language:"vi", height:'100%', width:'100%'});
	CKFinder.setupCKEditor(editor, 'admin/ckfinder') ;
	new JsDatePick({
        useMode:2,
        target:"m_start",
        dateFormat:"%Y-%m-%d"
    });
	new JsDatePick({
        useMode:2,
        target:"m_end",
        dateFormat:"%Y-%m-%d"
    });
    
};
$(document).ready(function(){
    $('#dprice').priceFormat({
        prefix: '',
        centsLimit: 0,
        centsSeparator: '',
        thousandsSeparator: ','
	
    });
});

function getFormattedDate(year,month,day) {
	  month = month.length > 1 ? month : '0' + month;
	  day = day.length > 1 ? day : '0' + day;
	  return year + '-' + month + '-' + day;
}

function onchangetime(co){
	if(co==1){
		var d= new Date();
		document.getElementById('m_start').value=getFormattedDate(d.getFullYear(),(d.getMonth()+1).toString(),d.getDate().toString());
		document.getElementById('m_end').value=getFormattedDate(d.getFullYear(),(d.getMonth()+1).toString(),d.getDate().toString());
	}
	if(co==-1){
		var d= new Date();
		d.setDate(d.getDate() -1);
		document.getElementById('m_end').value= getFormattedDate(d.getFullYear(),(d.getMonth()+1).toString(),d.getDate().toString());
		document.getElementById('m_start').value=getFormattedDate(d.getFullYear(),(d.getMonth()+1).toString(),d.getDate().toString());
		
	}
	if(co==7){
		var d= new Date();
		document.getElementById('m_end').value= getFormattedDate(d.getFullYear(),(d.getMonth()+1).toString(),d.getDate().toString());
		d.setDate(d.getDate() -7);
		document.getElementById('m_start').value=getFormattedDate(d.getFullYear(),(d.getMonth()+1).toString(),d.getDate().toString());
		
	}
	if(co==15){
		var d= new Date();
		document.getElementById('m_end').value= getFormattedDate(d.getFullYear(),(d.getMonth()+1).toString(),d.getDate().toString());
		d.setDate(d.getDate() -15);
		document.getElementById('m_start').value=getFormattedDate(d.getFullYear(),(d.getMonth()+1).toString(),d.getDate().toString());
		
	}
	if(co==30){
		var d= new Date();
	/*d.setDate(d.getDate() - 1);*/
		document.getElementById('m_end').value= getFormattedDate(d.getFullYear(),(d.getMonth()+1).toString(),d.getDate().toString());
		d.setMonth(d.getMonth() - 1);
		document.getElementById('m_start').value=getFormattedDate(d.getFullYear(),(d.getMonth()+1).toString(),d.getDate().toString());
		
	}
	if(co==-30){
		var d= new Date();
		document.getElementById('m_end').value= getFormattedDate(d.getFullYear(),(d.getMonth()+1).toString(),d.getDate().toString());
		//d.setMonth(d.getMonth() - 1);
		document.getElementById('m_start').value=getFormattedDate(d.getFullYear(),(d.getMonth()+1).toString(),"01");
		
	}
}
function submit() {
	var ff = document.mform;
	ff.submit();
}

function enterTable(e,value){
    var key = window.event ? e.keyCode : e.which;
    if(key==13){
        if(value=="submit") submit();
        else{
    	document.getElementById(value).focus();
		document.getElementById(value).select();
		return false;
        } 
	}else
		return true;	
}

function changePrice(){
	var value=document.getElementById('dprice').value;
	var sale_off=document.getElementById('dsale_off').value;
	if(sale_off=="")
		sale_off=0;
	if(value=="")
		value=0;
	var b=value.split(',');
    if(b.length>1){
    	value='';
        for(i=0;i<b.length;i++){
        	value=value+b[i];
        }
    }
	value=parseFloat(value);

	document.getElementById('dprice_sale_off').value=numberWithCommas(Math.round((value-(value*parseFloat(sale_off/100)))/1000)*1000);
}

function changeSale_off(){
	var value=document.getElementById('dprice').value;
	var sale_off=document.getElementById('dsale_off').value;
	if(sale_off=="")
		sale_off=0;
	if(value=="")
		value=0;
	var b=value.split(',');
    if(b.length>1){
    	value='';
        for(i=0;i<b.length;i++){
        	value=value+b[i];
        }
    }
	value=parseFloat(value);

	document.getElementById('dprice_sale_off').value=numberWithCommas(Math.round((value-(value*parseFloat(sale_off/100)))/1000)*1000);
}
function numberWithCommas(x) {
	x=Math.round(x);
    return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}

function joinSubmit() {

		var ff = document.nf;

		if (ff.dname.value=="" || ff.dname.value==null)
		{
			alert("Điền vào tên Deal");
			ff.dname.focus();
			return;
		}
	

		if (ff.dsmall_image.value=="" || ff.dsmall_image.value==null)
		{
			alert("Fill Small Image Deal");
			ff.dsmall_image.focus();
			return;
		}else{
			var input, file;
		    if (!window.FileReader) {
		        alert("The file API isn't supported on this browser yet.");
		        return;
		    }
		    input = ff.dsmall_image;
		    if (!input) {
		    	 alert( "Um, couldn't find the fileinput element.");
		    }
		    else if (!input.files) {
		    	 alert( "This browser doesn't seem to support the `files` property of file inputs.");
		    }
		    else if (!input.files[0]) {
		    	 alert("Please select a file before clicking 'Load'");
		    }
		    else {
		       
		        var FileUploadPath=input.value;
		        var Extension = FileUploadPath.substring(FileUploadPath.lastIndexOf('.') + 1).toLowerCase();
	            if (Extension == "gif" || Extension == "png" || Extension == "bmp" || Extension == "jpeg" || Extension == "jpg") {
	            	file = input.files[0];
	            	//if(file.size>1048576){
	    	        //	alert("File " + file.name + " is too large. File must be less than 1 megabytes.");
	    	        //	co=1; return;
	    	        //}
	            }
	            else {
	                alert('Invalid file type. Only JPG, GIF and PNG types are accepted.');
	                return;
	            }
		        
		    }
		}


		
		if (ff.dlarge_image.value=="" || ff.dlarge_image.value==null)
		{
			alert("Fill Large Image Deal");
			ff.dlarge_image.focus();
			return;
		}else{
			var input, file;
		    if (!window.FileReader) {
		        alert("The file API isn't supported on this browser yet.");
		        return;
		    }
		    input = ff.dlarge_image;
		    if (!input) {
		    	 alert( "Um, couldn't find the fileinput element.");
		    }
		    else if (!input.files) {
		    	 alert( "This browser doesn't seem to support the `files` property of file inputs.");
		    }
		    else if (!input.files[0]) {
		    	 alert("Please select a file before clicking 'Load'");
		    }
		    else {
		       
		        var FileUploadPath=input.value;
		        var Extension = FileUploadPath.substring(FileUploadPath.lastIndexOf('.') + 1).toLowerCase();
	            if (Extension == "gif" || Extension == "png" || Extension == "bmp" || Extension == "jpeg" || Extension == "jpg") {
	            	file = input.files[0];
	            	//if(file.size>1048576){
	    	        //	alert("File " + file.name + " is too large. File must be less than 1 megabytes.");
	    	        //	co=1; return;
	    	        //}
	            }
	            else {
	                alert('Invalid file type. Only JPG, GIF and PNG types are accepted.');
	                return;
	            }
		        
		    }
		}
	
		
		ff.submit();
	
}
</script>

<div class="content_table_detail">
<table cellspacing="0" cellpadding="0" border="0" style="border-collapse: collapse;margin-left: -10px;margin-top: -20px;width: 980px;background: url('<?php echo base_url('images/title_03.gif') ?>') repeat-x scroll 0% 0% transparent;" >
				<thead>
					<tr>
						<th style="text-align: left;background: url('<?php echo base_url('images/title_03.gif') ?>') repeat-x scroll 0% 0% transparent;height: 24px;">Thêm Deal</th>
						
					</tr>
				</thead>
				</table>
				<br>
				<div class="content_table">
			<div class="search_box">
			<form name="mform" method="GET" action="<?=base_url('listdeal')?>">
				<div class="search_sbox">
					<ul>
						<li style="margin-top: 10px">Ngày tạo</li>
						
						<li><input name="m_start" id="m_start" type="text" value="<?=$m_start?>" readonly="readonly" style="width: 100px;">
						~<input name="m_end" id="m_end" type="text" value="<?=$m_end?>" readonly="readonly" style="width: 100px;"></li>
						
						<li class="divBtn" onclick="onchangetime(1)">Hôm nay</li>
						<li class="divBtn" onclick="onchangetime(-1)">Hôm qua</li>
						<li class="divBtn" onclick="onchangetime(-30)">Tháng này</li>
						<li class="divBtn" onclick="onchangetime(7)">Một tuần</li>
						<li class="divBtn" onclick="onchangetime(15)">Hai tuần</li>
						<li class="divBtn" onclick="onchangetime(30)">Một tháng</li>
					</ul>
					
					<ul>
						<li style="margin-top: 10px">Tên Deal</li>
						<li><input style="width: 550px;" name="m_namedeal" type="text" onkeypress="return enterTable(event,'m_category');" value=""></li>
					</ul>
					<ul>
						<li style="margin-top: 10px">Danh mục</li>
						<li><input style="margin-right: 150px;" onkeypress="return enterTable(event,'m_writer');" id="m_category" name="m_category" type="text" value=""></li>
						<li><div class="divBtn" style="margin-right:15px" onclick="submit();">Tìm</div></li>
					</ul>
					
				</div>
			</form>
			</div>
			</div>
			<br>
			<form name="nf" method="POST" enctype="multipart/form-data" action="<?=base_url('add_deal/add_deal_ok')?>">
			<table cellspacing="0" cellpadding="0" border="0" >
				<colgroup>
					<col width="100px">
					<col width="300px">
				</colgroup>
				<tbody>
				
					
					<tr>
						<th>Danh mục</th>
						<td colspan="3">
							<div class="contBox">
							
							<select name="did_category" style="height: 24px;width: 350px;">
									<?php 
									foreach ($list_category as $value){
											if($value['id_parent']==0){
												echo "<option value='".$value['id']."'>".$value['nameVN']."</option>";
												foreach ($list_category as $value1){
													if($value1['id_parent']==$value['id']){
														echo "<option value='".$value1['id']."'>---> ".$value1['nameVN']."</option>";
													}
												}
											}
									}
									
									?>
								</select>
								
							</div>
						</td>
					</tr>
					
					<tr>
						<th style="width: 80px;">Tên Deal</th>
						<td colspan="3">
							<div class="contBox"><input name="dname" id="dname" type="text"   style="width:350px;"></div>
						</td>
					</tr>
					
					
					<tr>
						<th style="width: 80px;">Giá</th>
						<td colspan="3">
							<div class="contBox"><input name="dprice" id="dprice" type="text" value="0" onchange="changePrice()" onkeypress="changePrice()" onkeyup="changePrice()" style="width: 100px"> VNĐ </div>
						</td>
					</tr>
					<tr>
						<th>Giảm giá</th>
						<td>
							<div class="contBox">
								<input name="dsale_off" id="dsale_off" type="text" onchange="changeSale_off()" onkeypress="changeSale_off()"  onkeyup="changeSale_off()" value="0" style="width: 30px;">%
							</div>
						</td>
						<th style="width: 80px;">Giá Khuyến Mãi</th>
						<td >
							<div class="contBox"><input name="dprice_sale_off" id="dprice_sale_off" type="text" readonly="readonly" value="0" style="width: 100px">  VNĐ</div>
						</td>
					</tr>
					<tr>
						<th>Hình nhỏ</th>
						<td colspan="3">
							<div class="contBox"><input name="dsmall_image" type="file"></div>
							<div class="contBox" style="font-size:10px;">* Khuyến khích hình ảnh có chiều ngang là 640.</div>
						</td>
					</tr>
					<tr>
						<th>Hình lớn</th>
						<td colspan="3">
							<div class="contBox"><input name="dlarge_image" type="file"></div>
							<div class="contBox" style="font-size:10px;">* Khuyến khích hình ảnh có chiều ngang là 640.</div>
						</td>
					</tr>
					
					<tr>
						<th>Tiêu đề sản phẩm</th>
						<td colspan="3">
							<div class="contBox">
													<textarea name="dtitle" id="dtitle" style="width:840px; height:80px;line-height: 24px;"></textarea>
							
							</div>
						</td>
					</tr>
					
					<tr>
						<th>Ưu điểm sản phẩm</th>
						<td colspan="3">
							<div class="contBox">
													<textarea name="dadvantage" id="dadvantage" style="width:700px; height:180px;line-height: 24px;"></textarea>
							
							</div>
						</td>
					</tr>
					
					<tr>
						<th>Điều kiện sử dụng</th>
						<td colspan="3">
							<div class="contBox">
													<textarea name="dcondition" id="dcondition" style="width:700px; height:180px;line-height: 24px;"></textarea>
							
							</div>
						</td>
					</tr>
					
					<tr>
						<th>Chi tiết sản phẩm</th>
						<td colspan="3">
							<div class="contBox">
									<textarea name="dcontent" id="dcontent" style="width:700px; height:180px;line-height: 24px;"></textarea>
							</div>
						</td>
					</tr>
					<tr>
						<th style="width: 80px;">Đánh giá</th>
						<td colspan="3">
							<div class="contBox">
								<label><input name="top_seller" value="on"  type="checkbox" >Top Seller</label>
								<label><input name="top_view" value="on" type="checkbox"  >Top view</label>
								<label><input name="top_100" value="on" type="checkbox"  >Top 100</label>
							</div>
						</td>
					</tr>
					<tr>
						<th style="width: 80px;">Vận chuyển</th>
						<td colspan="3">
							<div class="contBox">
							<label><input name="freeship" value="on" type="checkbox" checked="checked">Free Ship</label>
							<label><input name="delivery"  type="radio" value="0" checked="checked" >Giao sản phẩm</label>
							<label><input name="delivery"  type="radio"  value="1" >Giao voucher</label>
							
							</div>
						</td>
					</tr>
				</tbody>
				<tfoot>
					<tr>
						<td colspan="4">
							<div class="divBtn" style="width:100px; float:right; margin:0px 5px;" onclick="joinSubmit()">Lưu</div> 
							<div class="divBtn" style="width:100px; float:right;" onclick="history.back()">Trở lại</div> 
						</td>
					</tr>
				</tfoot>
			</table>

			</form>
			</div>
	